package com.pshcp.service;

import cn.hutool.core.collection.CollectionUtil;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorScheduleDTO;
import com.pshcp.domain.model.DoctorSchedule;
import com.pshcp.mapper.DoctorScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生排班表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-08
 */
@Service
public class DoctorScheduleService {

    @Autowired
    private DoctorScheduleMapper doctorScheduleMapper;

    public PageList<DoctorSchedule> pageList(DoctorScheduleDTO doctorScheduleDTO){
        if(doctorScheduleDTO.getPageNum() != null && doctorScheduleDTO.getPageSize() != null){
            PageHelper.startPage(doctorScheduleDTO.getPageNum(), doctorScheduleDTO.getPageSize());
        }
        Example example = new Example(DoctorSchedule.class);
        Example.Criteria criteria = example.createCriteria();
        if(doctorScheduleDTO.getStatus() != null){
            criteria.andEqualTo("status", doctorScheduleDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorSchedule> list = doctorScheduleMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorSchedule> list(DoctorScheduleDTO doctorScheduleDTO){
        Example example = new Example(DoctorSchedule.class);
        Example.Criteria criteria = example.createCriteria();
        if(doctorScheduleDTO.getStatus() != null){
            criteria.andEqualTo("status", doctorScheduleDTO.getStatus());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getAccessDeptId())){
            criteria.andEqualTo("accessDeptId", doctorScheduleDTO.getAccessDeptId());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getAccessDoctorId())){
            criteria.andEqualTo("accessDoctorId", doctorScheduleDTO.getAccessDoctorId());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getSchDate())){
            criteria.andEqualTo("schDate", doctorScheduleDTO.getSchDate());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getStartTime())){
            criteria.andGreaterThanOrEqualTo("schDate", doctorScheduleDTO.getStartTime());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getEndTime())){
            criteria.andLessThan("schDate", doctorScheduleDTO.getEndTime());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getDayType())){
            criteria.andEqualTo("dayType", doctorScheduleDTO.getDayType());
        }
        if(!ObjectUtils.isEmpty(doctorScheduleDTO.getIsExpect())){
            criteria.andEqualTo("isExpect", doctorScheduleDTO.getIsExpect());
        }
        example.setOrderByClause(" sch_date asc");
        return doctorScheduleMapper.selectByExample(example);
    }

    public List<DoctorSchedule> select(DoctorSchedule doctorSchedule){
        return doctorScheduleMapper.select(doctorSchedule);
    }

    public int insert(DoctorSchedule doctorSchedule){
        doctorSchedule.setId(SystemUtil.getId());
        doctorSchedule.setStatus(CommonStatusEnum.INIT.getCode());
        doctorSchedule.setCreatedTime(new Date());
        return doctorScheduleMapper.insert(doctorSchedule);
    }

    public int update(DoctorSchedule doctorSchedule){
        doctorSchedule.setUpdateTime(new Date());
        return doctorScheduleMapper.updateByPrimaryKeySelective(doctorSchedule);
    }

    public DoctorSchedule findById(Long id){
        return doctorScheduleMapper.selectByPrimaryKey(id);
    }

    public List<DoctorSchedule> findByIds(List<Long> ids){
        Example example = new Example(DoctorSchedule.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorScheduleMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DoctorSchedule.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorSchedule doctorSchedule = new DoctorSchedule();
        doctorSchedule.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorScheduleMapper.updateByExampleSelective(doctorSchedule, example);
    }

    public DoctorSchedule queryByAccessSchId(String accessSchId){
        Example example = new Example(DoctorSchedule.class);
        Example.Criteria criteria = example.createCriteria();
        if(!ObjectUtils.isEmpty(accessSchId != null)){
            criteria.andEqualTo("accessSchId", accessSchId);
        }
        List<DoctorSchedule> lstData = doctorScheduleMapper.selectByExample(example);
        if(CollectionUtil.isEmpty(lstData)){
            return null;
        }
        return lstData.get(0);
    }
}
